angular.module('mamApp').controller('specialDimensionModalController', ['$scope', '$state', '$uibModal', 'ImsDialog', 'TagsManager', '$filter', '$uibModalInstance', 'data', 'channelList',
    function ($scope, $state, $uibModal, ImsDialog, TagsManager, $filter, $uibModalInstance, data, channelList) {

        $scope.formData = data;
        $scope.channelList = channelList;
        //所属频道的选择;
        $scope.isShow=channelList[0].categoryId;
        $scope.show = function (categoryId) {
            $scope.isShow = categoryId;
        };

        $scope.table = JSON.parse(localStorage.getItem("codeTable"));

        $scope.initData = {
            "status": ["4", "2"]
        };

        //新建确定(维度名称是否存在后台判断)
        $scope.okCreate = function () {
            var array = [];
            $scope.channelList.forEach(function (item) {
                if (item.list){
                    item.list.forEach(function (data) {
                        if (data.checked) {
                            array.push(data.channelId)
                        }
                    })
                }
            });
            $scope.formData.ids = [$scope.formData.channelId];

            TagsManager.specialDimensionsAdd($scope.formData).then(function (result) {
                if (result.data.c == "s") {
                    $uibModalInstance.close("ok");
                } else {
                    $scope.nameCheck = true;
                    $scope.message=result.data.i;
                }
            });
        };

        //获取编辑里面“所属频道列表” 选中的值，展示出来checked
        if ($scope.formData.dimensionId) {
            TagsManager.specialEditChannelList($scope.formData.dimensionId).then(function (result) {
                var ids = result.data.sdimensionVO.ids;

                $scope.channelList.forEach(function (item) {
                    if (item.list){
                        item.list.forEach(function (data) {
                            if (ids.indexOf(data.channelId.toString()) > -1) {
                                data.checked = true;
                            }
                        })
                    }
                })
            });
        }

        //编辑确定(维度名称是否存在后台判断)
        $scope.okEdit = function () {
            var array = [];
            $scope.channelList.forEach(function (item) {
                if (item.list){
                    item.list.forEach(function (data) {
                        if (data.checked) {
                            array.push(data.channelId)
                        }
                    })
                }
            });
            $scope.formData.ids = array;

            TagsManager.specialDimensionsEdit($scope.formData.dimensionId, $scope.formData).then(function (result) {
                if (result.data.c == "s") {
                    $uibModalInstance.close("ok");
                } else {
                    $scope.nameCheck = true;
                    $scope.message=result.data.i;
                    //$uibModalInstance.close("error");
                }
            });
        };

        $scope.cancel = function () {
            $uibModalInstance.close("cancel");
        };
    }]);

